package com.mybatisplus.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * @ProjectName mybatis-generator
 * @ClassName CodeAutoGenerator
 * @Author: Ocelot
 * @Date: 2021/10/13 14:49
 * @Description:
 **/
public class CodeAutoGenerator {

    public static final String OUTPUT_DIR = "E:\\git\\Plugins\\mybatisplus-generator\\mybatisplus-generator-workspace";
    public static final String XML_OUTPUT_DIR = "E:\\git\\Plugins\\mybatisplus-generator\\mybatisplus-generator-workspace\\com\\boot\\mapper\\mapping";
    public static final String[] TABLE_NAME = {
            "product",
            "stu_info",
    };
    /**
     * 待生成表中需要去除的前缀(写入该数组中的前缀将被去掉，不会再类名中出现)
     * 原表名为 t_system，直接生成类名为 TSystem，配置后为System，将去除以 t_这一开头
     */
    public static final String[] REMOVE_TABLE_PRE = {
            "t_",
            "c_",
    };
    public static final String URL = "jdbc:mysql://127.0.0.1:3306/promotion?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "root";
    /**
     * controller、service、mapper三个包位于的父级包路径
     */
    public static final String FATHER_PACKAGE = "com.boot";
    /**
     * controller、service、mapper三个包位于的父级包名称，若不为空则会多出一级包
     */
    public static final String FATHER_MODULE_NAME = "";

    public static void main(String[] args) {

        /**
         * 数据源配置
         */
        DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
                .Builder(URL, USERNAME, PASSWORD);

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder -> {
                    builder.author("Ocelot") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(OUTPUT_DIR); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(FATHER_PACKAGE) // 设置父包名
                            .moduleName(FATHER_MODULE_NAME) // 设置父包模块名
                            .entity("pojo.dto")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.mapping")
                            .controller("controller")
                            .other("other")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, XML_OUTPUT_DIR)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLE_NAME)
                            .addTablePrefix(REMOVE_TABLE_PRE)
                            .entityBuilder()
                            .superClass(BaseDto.class)
                            .formatFileName("%sDto")
                            .enableChainModel()
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .controllerBuilder()
                            .enableRestStyle()
                            .formatFileName("%sController")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                .templateConfig(builder -> {
                    /**
                     * 配置生成文件的模板
                     */
                    builder.entity("/generators/entity.java")
                            .service("/generators/service.java")
                            .serviceImpl("/generators/serviceImpl.java")
                            .mapper("/generators/mapper.java")
                            .mapperXml("/generators/mapper.xml")
                            .controller("/generators/controller.java");
                })
                .injectionConfig(builder -> {
                    builder.beforeOutputFile((tableInfo, objectMap) -> {
                        System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                    });
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
